package com.foreveross.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.foreveross.common.CommonUtil;
import com.foreveross.common.Constants;
import com.foreveross.service.LogService;
import com.foreveross.service.NewsHouseService;
import com.foreveross.util.AuthUtil;
import com.foreveross.vo.HrNewsInfo;
import com.foreveross.vo.NewsHouse;
import com.foreveross.vo.common.PageVo;
import com.foreveross.vo.common.ResultVo;

@Controller
@RequestMapping(value = "/sysNewsHouseController")
public class SysNewsHouseController extends BaseCostroller {

	private Logger log = Logger.getLogger(SysNewsHouseController.class);
	
	@Resource
	private NewsHouseService newsHouseService;
	
	@Resource
	private LogService logService;
	
	@RequestMapping(method = RequestMethod.POST, value = "/addNewsInfo")
	public void addNewsInfo(HttpServletRequest request,
			HttpServletResponse response,HrNewsInfo newsInfo,String houseIds){
		ResultVo resultVo = new ResultVo();
		try {
			newsHouseService.addHrNewsInfo(newsInfo, houseIds);
			resultVo.setSuccess(true);
			logService.saveLog(this.getUserIP(request),Constants.OPERATE_TYPE_BACKGROUND,Constants.LOG_ADD, 
					"资讯关联楼盘"+Constants.getUpdateType(Constants.UPDATE_ADD),AuthUtil.getUserid(request), 
					Constants.getUpdateType(Constants.UPDATE_ADD)+"资讯关联楼盘[" + newsInfo.toString()
							+ "]");
			log.info("资讯关联楼盘 "+Constants.UPDATE_ADD+"成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("资讯 关联楼盘"+Constants.UPDATE_ADD+"失败");
			log.error("资讯关联楼盘"+Constants.UPDATE_ADD+"失败!",e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	@RequestMapping(method = RequestMethod.POST, value = "/getNewsHouseList")
	public void getNewsHouseList(HttpServletRequest request,
			HttpServletResponse response, String newsId,String houseId,
			String page,String pageSize) {
		ResultVo resultVo = new ResultVo();
		try {
			int total = (int)newsHouseService.getNewsHouseListCount(newsId,houseId);
			PageVo<NewsHouse> pageVo = new PageVo<NewsHouse>(page,total,pageSize);
			List<NewsHouse> list = newsHouseService.getNewsHouseList(newsId,houseId,pageVo.getpageIndex(),pageVo.getpageSize());
			pageVo.setRows(list);
			resultVo.setSuccess(true);
			resultVo.setData(pageVo);
			log.info("资讯关联楼盘 查看成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("资讯关联楼盘  查看失败");
			log.error("资讯关联楼盘 查看失败!", e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	@RequestMapping(method = RequestMethod.POST, value = "/deleteNewsHouse")
	public void deleteNewsHouse(HttpServletRequest request,
			HttpServletResponse response, 
			String ids) {
		ResultVo resultVo = new ResultVo();
		try{
			newsHouseService.deleteNewsHouse(ids);
			resultVo.setSuccess(true);
			logService.saveLog(this.getUserIP(request),Constants.OPERATE_TYPE_BACKGROUND,Constants.LOG_DEL, "通知消息关联楼盘删除",
					AuthUtil.getUserid(request), 
					"资讯关联楼盘[" + ids+ "]");
			log.info("资讯关联楼盘 删除成功!");
		}catch(Exception e){
			resultVo.setSuccess(false);
			resultVo.setMessage("资讯关联楼盘 删除失败");
			log.info("资讯关联楼盘 删除失败!");
		}
		
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
}
